import 'dart:convert';

import '../../../../data/models/auth/user.dart';
import '../../../exercises/exercises_page/models/exercise.dart';
import '../../../gyms/gyms_page/models/gym.dart';

//Subscription subscriptionFromJson(String str) => Subscription.fromJson(json.decode(str));

//String subscriptionToJson(Subscription data) => json.encode(data.toJson());

class Subscription {
  final String? subscriptionId;
  final String userId;
  final DateTime startDate;
  final DateTime endDate;
  final String exerciseId;
  final String? createdBy;
  final DateTime? createAt;
  final DateTime? updateAt;
  final User? user;
  final Exercise? exercise;
  final Gym? gym;

  Subscription({
    this.subscriptionId,
    required this.userId,
    required this.startDate,
    required this.endDate,
    required this.exerciseId,
    this.createdBy,
    this.createAt,
    this.updateAt,
    this.user,
    this.exercise,
    this.gym,
  });

  Subscription copyWith({
    String? subscriptionId,
    String? userId,
    DateTime? startDate,
    DateTime? endDate,
    String? exerciseId,
    String? createdBy,
    DateTime? createAt,
    DateTime? updateAt,
    User? user,
    Exercise? exercise,
    Gym? gym,
  }) =>
      Subscription(
        subscriptionId: subscriptionId ?? this.subscriptionId,
        userId: userId ?? this.userId,
        startDate: startDate ?? this.startDate,
        endDate: endDate ?? this.endDate,
        exerciseId: exerciseId ?? this.exerciseId,
        createdBy: createdBy ?? this.createdBy,
        createAt: createAt ?? this.createAt,
        updateAt: updateAt ?? this.updateAt,
        user: user ?? this.user,
        exercise: exercise ?? this.exercise,
        gym: gym ?? this.gym,
      );

  factory Subscription.fromJson(Map<String, dynamic> json) => Subscription(
    subscriptionId: json["subscription_id"]?.toString(),
    userId: json["user_id"].toString(),
    startDate: DateTime.parse(json["start_date"]),
    endDate: DateTime.parse(json["end_date"]),
    exerciseId: json["exercise_id"].toString(),
    createdBy: json["created_by"]?.toString(),
    createAt: DateTime.parse(json["create_at"]),
    updateAt: DateTime.parse(json["update_at"]),
    user:json["user"] == null ?json["user"]: User.fromJson(json["user"]),
    exercise:json["exercise"] == null ?json["exercise"]: Exercise.fromJson(json["exercise"]),
    gym:json["gym"] == null ?json["gym"]: Gym.fromJson(json["gym"]),
  );

  Map<String, dynamic> toJson() => {
    "subscription_id": subscriptionId,
    "user_id": userId,
    "start_date": startDate.toIso8601String(),
    "end_date": endDate.toIso8601String(),
    "exercise_id": exerciseId,
    "created_by": createdBy,
    "create_at": createAt?.toIso8601String(),
    "update_at": updateAt?.toIso8601String(),
    "user": user?.toJson(),
    "exercise": exercise?.toJson(),
    "gym": gym?.toJson(),
  };
}

